﻿#pragma once
#include<assert.h>
#include<iostream>//c++的输入输出流
using std::cout, std::endl;
namespace myspace
{
	/*list节点的模版函数*/
	template<class T>
	struct __list_node
	{
		/*成员变量*/
		__list_node<T>* _next;
		__list_node<T>* _prev;
		T _data;
		/*构造*/__list_node(const T& x = T())
			:_next(nullptr),_prev(nullptr),_data(x)
		{}
	};
	/*___________________*/

	/*list迭代器的模版函数*/
	template<class T,class Ref,class Ptr>
	struct __list_iterator
	{
		typedef __list_node<T> Node; 
		typedef __list_iterator<T, Ref, Ptr> Self;
		/*成员变量*/
		Node* _node;

		/*构造*/__list_iterator(Node* node)
			:_node(node){ }

		/*解引用符号重载*/Ref operator*()
		{
			return _node->_data;
		}
		/*->符号重载*/Ptr operator->()
		{
			return &_node->_data;
		}
		/*++符号重载*/Self& operator++()
		{
			_node=_node->_next;
			return *this;
		}
		/*--符号重载*/Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		/*++符号重载(后置)*/Self operator++(int)
		{
			Self tmp(*this);
			_node = _node->_next;
			return tmp;
		}
		/*--符号重载(后置)*/Self operator--(int)
		{
			Self tmp(*this);
			_node = _node->_prev;
			return tmp;
		}
		/*!=符号重载*/bool operator!=(const Self& it)
		{
			return _node != it._node;
		}
		/*==符号重载*/bool operator==(const Self& it)
		{
			return _node == it._node;
		}
	};
	/*___________________*/

	/*模拟list的模版函数*/
	template<class T>//模版类的函数声明和定义要放在一起
	class list
	{
		typedef __list_node<T> Node;

	public:
		/*普通迭代器*/typedef  __list_iterator<T,T&,T*> iterator;
		/*const迭代器*/typedef  __list_iterator<T, const T&, const T*> const_iterator;
		/*普通begin()*/iterator begin()
		{
			return iterator(_head->_next);
		}
		/*普通end()*/iterator end()
		{
			return iterator(_head);
		}//__________________
		/*const修饰begin()*/const_iterator begin()const
		{
			return const_iterator(_head->_next);
		}
		/*const修饰end()*/const_iterator end()const
		{
			return const_iterator(_head);
		}//__________________
		/*显示调用的cbegin()*/const_iterator cbegin()const
		{
			return begin();
		}
		/*显示调用的cend()*/const_iterator cend()const
		{
			return end();
		}//__________________

		/*构造*/list()
		{
			_head = new Node; 
			_head->_next = _head;
			_head->_prev = _head;
		}
		/*拷贝构造*/list(const list<T>& lt)
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			for(auto e:lt)push_back(e);
		}
		/*析构*/~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}
		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				erase(it++);
			}
		}//__________________

		///*赋值(传统写法)*/list<T>& operator=(const list<T>& lt)
		//{
		//	if (this != &lt)
		//	{
		//		clear();
		//		for (auto e : lt)push_back(e);
		//	}
		//	return *this;
		//}
		/*赋值（现代写法）*/list<T>& operator=(list<T> lt)
		{
			using std::swap;
			swap(_head, lt._head);
			return *this;
		}
		/*尾插*/void push_back(const T& x)
		{
			/*Node* tail = _head->_prev;
			Node* newnode = new Node(x);

			tail->_next = newnode;
			newnode->_prev = tail;
			newnode->_next = _head;
			_head->_prev = newnode;*/
			//insert(_head, x);
			insert(end(), x);
		}
		/*尾删*/void pop_back()
		{
			//erase(_head->_prev);
			//erase(iterator(_head->_prev));
			erase(--end());
		}
		/*头插*/void push_front(const T& x)
		{
			insert(begin(), x);
		}
		/*头删*/void pop_front()
		{
			erase(begin());
		}
		/*指定位置插入*/void insert(iterator pos, const T& x)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* newnode = new Node(x);

			newnode->_next = cur;
			newnode->_prev = prev;
			prev->_next = newnode;
			cur->_prev = newnode;
		}
		/*指定位置删除*/iterator erase(iterator pos)
		{
			assert(pos != end());
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;

			prev->_next = next;
			next->_prev = prev;
			delete cur;
			return iterator(next);
		}



	private:
		Node* _head;
	};
	/*___________________*/


	void print_list(const list<int>& lt)
	{
		list<int>::const_iterator it = lt.cbegin();
		while (it != lt.cend())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}
	void test_list1()
	{
		cout << "test_list1" << endl;
		list<int> l1;
		l1.push_back(1);
		l1.push_back(2);
		l1.push_back(3);
		l1.push_back(4);
		print_list(l1);
		list<int>::iterator it = l1.begin();
		it=l1.erase(++it);
		while (it != l1.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
		print_list(l1);
		it = l1.begin();
		l1.insert(++it, 5);
		print_list(l1);
		l1.pop_back();
		print_list(l1);
		l1.pop_front();
		print_list(l1);

		cout << "test_list1" << endl;
	}
	void test_list2()
	{
		cout << "test_list2" << endl;
		list<int> l1;
		l1.push_back(1);
		l1.push_back(2);
		l1.push_back(3);
		l1.push_back(4);
		print_list(l1);
		list<int> l2(l1);
		print_list(l2);
		list<int> l3;
		list<int> l4;
		print_list(l3);
		print_list(l4);
		l4 = l3 = l1;
		print_list(l3);
		print_list(l4);


		cout << "test_list2" << endl;
	}
	
}